import {
  formatDate,
  formatDate2,
  formatDuration
} from '../../utils/date'
const app = getApp()
const domain = app.globalData.domain

// pages/tenane-edit/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id: null,
    personVis: false,
    customerVis: false,
    data: {},
    personList: [],
    clientList: [],
    personData: {}
  },
  onBack() {
    wx.redirectTo({
      url: '../operation/index',
    })
  },

  closeDialog() {
    this.setData({
      personVis: false,
      customerVis: false
    })
  },

  setPersonVis(event) {
    console.log(event)
    this.setData({
      personVis: event.currentTarget.dataset.value ? true : false
    })
  },
  handleEdit(e) {
    const personData = e.currentTarget.dataset.odata
    console.log('personData', personData)
    this.setData({
      personVis: true,
      personData
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log('options', options)
    this.setData({
      id: options.id
    })
    this.init(options.id)
    this.initPersonList(options.id)
  },

  changePersonValue(e) {
    console.log(e)
    const value = e.detail.value
    console.log(e)
    const personData = this.data.personData;
    const {
      target
    } = e.currentTarget.dataset
    personData[target] = value
    console.log('data', personData, target, value)
    this.setData({
      personData
    })
  },

  personOpreation() {
    console.log('this.data.personData', this.data.personData)
    wx.request({
      url: this.data.personData.id ? `${domain}/admin-api/system/user/update` : `${domain}/admin-api/system/user/create`,
      method: this.data.personData.id ? "PUT" : "POST",
      data: {
        ...this.data.personData,
        // id: this.data.options.id
      },
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: res => {
        if (res?.data?.code == '401') {
          wx.removeStorageSync('token')
          wx.reLaunch({
            url: '../login/login',
          })
          return;
        }

        if (res?.data?.code == 0) {
          wx.showToast({
            title: '操作成功',
            icon: 'none'
          })
          this.initPersonList()
          this.closeDialog()
          this.setData({
            personData: {}
          })
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },
  personStatusChange(e) {
    const {
      id,
      status
    } = e.currentTarget.dataset

    wx.request({
      url: `${domain}/admin-api/system/user/update-status`,
      method: "PUT",
      data: {
        id,
        status: status == 1 ? 0 : 1
      },
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: res => {
        if (res?.data?.code == '401') {
          wx.removeStorageSync('token')
          wx.reLaunch({
            url: '../login/login',
          })
          return;
        }

        if (res?.data?.code == 0) {
          wx.showToast({
            title: '操作成功',
            icon: 'none'
          })
          this.initPersonList()
        } else {
          wx.showToast({
            title: res.data.msg,
            icon: 'none'
          })
        }
      }
    })
  },

  init(id) {
    wx.request({
      url: `${domain}/admin-api/system/tenant/get?id=${id}`,
      data: {

      },
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: res => {
        if (res?.data?.code == '401') {
          wx.removeStorageSync('token')
          wx.reLaunch({
            url: '../login/login',
          })
          return;
        }
        console.log('res', res)
        const data = res?.data?.data
        this.setData({
          data
        })
      }
    })
  },


  initPersonList(id) {
    wx.request({
      url: `${domain}/admin-api/system/user/page`,
      data: {
        pageNum: 1,
        pageSize: 100
      },
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: res => {
        if (res?.data?.code == '401') {
          wx.removeStorageSync('token')
          wx.reLaunch({
            url: '../login/login',
          })
          return;
        }
        console.log('res', res)
        const personList = res?.data?.data?.list
        this.setData({
          personList
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})